package uit.minito.timduongxebuyt;

import java.io.Serializable;

import uit.minito.database.HistoryDBAdapter;
import uit.minito.database.HistoryModel;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class HistoryViewActivity extends Activity{
	
	TextView titleTextView;
	ListView historyListView;
	HistoryDBAdapter historyAccess;
	HistoryArrayAdapter adapter;
	Button historyTrashButton;
	DialogInterface.OnClickListener dialogClickListener;
	private HistoryModel getHistoryAtPosition(int position){
		return adapter.getHistoryList().get(position);
	}
	public void openHistoryAtPosition(int position){
		HistoryModel history=getHistoryAtPosition(position);
		Intent intent = new Intent(HistoryViewActivity.this, Map2MainActivity.class);
		intent.putExtra("history", (Serializable)history);
	     startActivity(intent);
	}
	private Context getCurrentHistoryContext(){
		return this;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub 
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_history);
		titleTextView=(TextView)findViewById(R.id.historyTitleTextView);
		historyListView=(ListView)findViewById(R.id.historyListView);
		historyTrashButton=(Button)findViewById(R.id.historyTrashButton);
		historyAccess= new HistoryDBAdapter(this);
		//historyAccess.deleteAll();
		adapter=new HistoryArrayAdapter(this, historyAccess.loadAllHistory());
		historyListView.setAdapter(adapter);
		historyListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				openHistoryAtPosition(position);
				// TODO Auto-generated method stub
				
			}
		});
		registerForContextMenu(historyListView);
		dialogClickListener = new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int which) {
		        switch (which){
		        case DialogInterface.BUTTON_POSITIVE:
		            //Yes button clicked
		        	historyAccess.deleteAll();
		        	adapter.getHistoryList().clear();
		        	adapter.notifyDataSetChanged();  
		        	historyTrashButton.setEnabled(false);  
		            break;

		        case DialogInterface.BUTTON_NEGATIVE:
		            //No button clicked
		            break;
		        }
		    }
		};
		if(adapter.getHistoryList().size()==0 || adapter.getHistoryList()==null){
			historyTrashButton.setEnabled(false);
		}else{
			historyTrashButton.setEnabled(true);
		}
		historyTrashButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(getCurrentHistoryContext());
				builder.setMessage(R.string.dialog_title).setPositiveButton(R.string.dialog_yes, dialogClickListener)
				    .setNegativeButton(R.string.dialog_no, dialogClickListener).show();
			}
		});
	}

	
	
	
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.history_context, menu);
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.menu_open:
			openHistoryAtPosition((int)info.id);
			return true;
		case R.id.menu_delete:
			HistoryModel deleteModel=adapter.getHistoryList().get((int)info.id);
			historyAccess.deleteAtID(deleteModel.getHistoryID());
			adapter.getHistoryList().remove((int)info.id);
			adapter.notifyDataSetChanged();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
		
	}

}
